package hy.service;

import hy.beans.Account;
import hy.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl {
	@Autowired
	AccountDao dao;
	@Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class,noRollbackFor = NullPointerException.class,timeout = 5)
	public void transfer(String from, String to, Double money) {
		dao.update(from, 0-money);
		System.out.println("first---测试虚读"+dao.selectAllAccount());
		try {
			Thread.sleep(000);
		} catch (InterruptedException e) {}
//		System.out.println("last---测试虚读"+dao.update("a3",100.0));
		System.out.println("first---测试虚读"+dao.selectAllAccount());
		int a = 1/0;
		dao.update(to, money);
	}

	public void a(){
		this.transfer("a1","a2",100.0);
	}

	@Transactional(isolation = Isolation.SERIALIZABLE)
	public void query(String name) {
		System.out.println(name+"的余额："+dao.selectMoney(name));
	}

	
	public void insert() {
		System.out.println(dao.insert(new Account(null,"a3",500.0)));
	}
}